package ws;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import at.fhj.itm11.forum.model.User;

@WebService(serviceName = "FHJForumAuthWS")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class FHJForumAuthWS {
	@PersistenceContext
	private EntityManager em;
	
	@WebMethod
	public boolean authUser(
			@WebParam(name="username") String username, 
			@WebParam(name="password") String password) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> criteria = cb.createQuery(User.class);
        Root<User> User = criteria.from(User.class);
		criteria.select(User)
				.where(cb.equal(User.get("username"), username))
				.where(cb.equal(User.get("password"), password));
		List<User> results = em.createQuery(criteria).getResultList();
		
        return results != null && results.size() > 0;
	}
}
